package com.cy.pj.sys.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.pj.common.exception.ServiceException;
import com.cy.pj.common.vo.PageObject;
import com.cy.pj.sys.dao.SysLogDao;
import com.cy.pj.sys.entity.SysLog;
import com.cy.pj.sys.service.SysLogService;

/**
 *	业务的实现类
 */
@Service
public class SysLogServiceImpl implements SysLogService{
	@Autowired
	private SysLogDao dao;
	
	@Override
	public PageObject<SysLog> findPageObjects(String username, Integer pageCurrent) {
		//1.如果当前页码为0或者小于1抛出异常
		if(pageCurrent==null || pageCurrent<1)
			//IllegalArgumentException无效的参数异常
			throw new IllegalArgumentException("当前页码不正确");
		//2.查询总记录数
		//2.1执行查询
		int rows = dao.getRowCount(username);
		//2.2验证查询结果
		if(rows==0) 
			throw new ServiceException("系统没有查到对应记录");
		//2.2基于条件查询当前页记录
		int pageSize=3;//每3条一页
		int startIndex=(pageCurrent-1)*pageSize;
		List<SysLog> records = dao.findPageObject(username, startIndex, pageSize);
		//2.3返回结果
		return new PageObject<>(pageCurrent, pageSize, rows,records);
		
	}

	@Override
	public int deleteObjects(Integer... ids) {
		//1.判定参数合法性
		if(ids==null||ids.length==0)
			throw new IllegalArgumentException("请选择一个");
		//2.执行删除操作
		int rows;
		try {
			rows = dao.deleteObjects(ids);
		}catch(Throwable e) {
			//3.发出警报信息
			throw new ServiceException("系统故障 正在恢复.....");
		}
		//4.对结果进行验证
		if(rows==0)
			throw new ServiceException("没有这些记录");
		//5.返回结果
		return rows;
	}

	@Override
	public void saveObject(SysLog entity) {
		dao.insertObject(entity);
	}
}














